<template>
  <div v-drag>
    <div class="mask" @click="closePopup(false)"></div>
    <div class="edit_box" v-loading="loading">
      <div class="box_head">
        <p>还桶详情</p>
        <i @click="closePopup(false)"></i>
      </div>
      <div class="box_body">
        <div>
          <div class="line">
            <div class="g-mt-10">
              <a class="common_btn el-icon-delete" @click="clearNum()"></a>
              <a class="common_btn el-icon-circle-plus-outline" @click="addProNum()"></a>
            </div>
          </div>
          <div>
            <div v-for="(link,index) in proNum" :key="index" class="input-box g-mb-10" id="link"
                 @mouseenter="showDel(index)" @mouseleave="delNum=null">
              <div class="input_box">
                <div class="select_box" ref="input">
                  <el-select
                    filterable
                    ref="linkName"
                    v-model="link.productId"
                    @change="tipText=''"
                    placeholder="请选择商品">
                    <el-option
                      v-for="(item,index) in list"
                      :key="index"
                      :label="item.productName"
                      :value="item.id">
                    </el-option>
                  </el-select>
                </div>
                <div class="num_box">
                  <span class="minus_btn"  @click="deleteProduct(index)">
                    <i class="el-icon-minus" :class="{'lose':parseFloat(link.numbers) <=1}"></i>
                  </span>
                      <input class="num_show" type="numbers" v-model="link.numbers"  min="0"/>
                   <span class="plus_btn" @click="addProduct(index)">
                    <i class="el-icon-plus"></i>
                  </span>
                </div>
                <!--只有一条的时候，不显示删除-->
                <i class="delSingle el-icon-remove-outline" @click="delSingleInput(index)" v-if="index>0"></i>
              </div>
            </div>
          </div>
        </div>
        <p class="error_line">{{ tipText }}</p>
      </div>
      <div class="box_foot">
        <span @click="closePopup(false)">取消</span>
        <span @click="confirmEdit">确定</span>
      </div>
    </div>
  </div>
</template>

<script>
import {confirmDelivery} from "@/api/data/user/productSend";
import {getReturnProduct} from "@/api/data/user/product";

export default {
  props:['orderId','userId'],
  data() {
    return {
      loading: false,
      tipText:'',
      list:[],
      proNum: [{
        numbers:1,
        productId:''
      }],
      test: [],
      delNum:null,
    }
  },
  mounted() {
    this.getProduct();
  },
  methods:{
    getProduct(){
      getReturnProduct({userId: this.userId}).then((res)=>{
        this.list = res ? res: [];
      })
    },

    closePopup(refresh) {
      this.$emit('listenToChild',{refresh:refresh})
    },

    confirmEdit() {
      let that = this;
      let error = 0;
      that.proNum.forEach((item, index) => {
        if (!item.productId) {
          if (that.proNum.length>1){
            that.$refs.input[index].className = 'error_border';
            that.tipText = '请选择需要归还桶的商品或者删除不需要归还桶的商品';
          }
          error++;
        }
      })
      that.createUserOrder(error);
    },

    createUserOrder(type){
      this.loading = true;
      let param = {
        id:this.orderId,
      }
      if (type===0){
        param.productInfo = this.proNum
      }
      confirmDelivery(param).then(res=>{
        this.$message.success({
          message:'操作成功',
          duration:1500
        });
        this.closePopup(true);
        this.loading = false;
      }).catch((e)=>{
        this.loading = false;
      })
    },

    deleteProduct(index){
      let num = this.proNum[index].numbers;
      this.proNum[index].numbers = parseFloat(num) <=1?1: parseFloat(num)-1;
    },

    addProduct(index){
      this.proNum[index].numbers = parseFloat(this.proNum[index].numbers) + 1;
    },

    /*添加*/
    addProNum() {
      let cope = {numbers:1,productId:''};
      this.proNum.push(cope);
      this.tipText = '';
    },

    /*清除*/
    clearNum() {
      this.proNum = [];
      for (const i in this.$refs.input) {
        this.$refs.input[i].className = ''
      }
      let cope = {numbers:1,productId:''};
      this.proNum.push(cope);
      this.tipText = '';
    },

    // 单个删除
    delSingleInput(index) {
      this.proNum.splice(index, 1)
    },

    showDel(index) {
      this.delNum = index + 1;
      /*只有一条的时候，不加删除文字，可清空*/
      if (this.proNum.length == 1) {
        this.delNum = null
      }
    },
  }
}
</script>

<style scoped>

 .input_box {
  display: flex;
  align-items: center;
  position: relative;
  overflow: hidden;
  /*width: 400px;*/
}

.line .link-input {
  line-height: 36px;
  border-radius: 4px;
  outline: none;
}

* {
  box-sizing: border-box;
}

.g-mb-10 {
  margin-bottom: 10px;
}

.g-ml-15 {
  margin-left: 15px;
}

a {
  text-decoration: none;
}

.link-txt {
  margin-right: 12px;
  width: 15px;
  float: left;
}

.link-input {
  width: 250px;
  height: 36px;
  background-color: #ffffff;
  border-radius: 2px;
  border: solid 1px #dcdcdc;
  padding-left: 15px;
  float: left;
  display: block;
  font-size: 14px;
  padding-right: 35px;
}

.common_btn {
  font-size: 24px;
  cursor: pointer;
  color: #6687f9;
  margin-right: 5px;
}

.delSingle {
  position: absolute;
  top: 10px;
  right: 38px;
  cursor: pointer;
  font-size: 22px;
  color: #e40000;
}

 .num_box  {
   border: 1px solid #DCDFE6;
   border-radius: 4px;
   overflow: hidden;
   margin-left: 30px;
 }

.minus_btn,.plus_btn {
  width: 34px;
  display: inline-block;
  height:34px;
  line-height:34px;
  text-align:center ;
  cursor: pointer;
}

 .minus_btn i,.plus_btn i{
   color: #6687f9;
 }

.minus_btn {
  border-right: 1px solid #DCDFE6;
}

.plus_btn {
  border-left: 1px solid #DCDFE6;
}

.lose {
  color: #DCDFE6!important;
}

.num_show {
  max-width: 80px;
  min-width: 40px;
  line-height: 34px;
  text-align:center ;
  border: none;
  outline: none;
}
 .error_line {
   padding-left: 0;
   margin-top: 30px;
 }
</style>
